home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_dturb.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  760 b   |  36 lines

  1. /* dturb.sl
  2.  *
  3.  * turbulence displacement
  4.  */
  5.  
  6. #include "k3d_rmannotes.h"
  7.  
  8. displacement k3d_dturb(float Km = 0.1, freq = 10, flatness = 1)
  9. {
  10.   float magnitude, layer_mag;
  11.   point PP;
  12.   float width, cutoff, fade, f, turb, maxfreq = 16;
  13.  
  14.   /* compute turbulence */
  15.  
  16.   PP = transform("shader", P) * freq;
  17.  
  18.   width = filterwidth_point(PP);
  19.   cutoff = clamp(0.5 / width, 0, maxfreq);
  20.  
  21.   turb = 0;
  22.   for (f = 1; f < 0.5 * cutoff; f *= 2) 
  23.     turb += abs(snoise(PP * f)) / f;
  24.   fade = clamp(2 * (cutoff - f) / cutoff, 0, 1);
  25.   turb += fade * abs(snoise(PP * f)) / f;
  26.  
  27.   /* raise to power to create flat areas */
  28.  
  29.   magnitude = pow(turb, flatness);
  30.  
  31.   /* output */
  32.  
  33.   P += Km * magnitude * normalize(N);
  34.   N = calculatenormal(P);
  35. }
  36.